package com.ruoyi.project.service.impl;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.ruoyi.base.util.GenCodeUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.material.domain.OutboundOrder;
import com.ruoyi.project.domain.BomPartstRef;
import com.ruoyi.project.mapper.BomPartstRefMapper;
import com.ruoyi.project.mapper.PartstInfoMapper;
import com.ruoyi.project.vo.BomBulidFloorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.BomInfoMapper;
import com.ruoyi.project.domain.BomInfo;
import com.ruoyi.project.service.IBomInfoService;

/**
 * bom信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-29
 */
@Service
public class BomInfoServiceImpl implements IBomInfoService 
{
    @Autowired
    private BomInfoMapper bomInfoMapper;

    @Autowired
    PartstInfoMapper partstInfoMapper;
    @Autowired
    BomPartstRefMapper bomPartstRefMapper;

    /**
     * 查询bom信息
     * 
     * @param id bom信息主键
     * @return bom信息
     */
    @Override
    public BomInfo selectBomInfoById(Long id)
    {
        return bomInfoMapper.selectBomInfoById(id);
    }

    /**
     * 查询bom信息列表
     * 
     * @param bomInfo bom信息
     * @return bom信息
     */
    @Override
    public List<BomInfo> selectBomInfoList(BomInfo bomInfo)
    {
        return bomInfoMapper.selectBomInfoList(bomInfo);
    }

    /**
     * 新增bom信息
     * 
     * @param bomInfo bom信息
     * @return 结果
     */
    @Override
    public int insertBomInfo(BomInfo bomInfo)
    {
        bomInfo.setCreateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        bomInfo.setCreateBy(user.getUserName());
        String bomNo=GenCodeUtils.getBOMNo();
        bomInfo.setBomNo(bomNo);
        String string=bomInfoMapper.selectBomNameMax(bomInfo.getProjectNo(),bomInfo.getCompomentNo());
        if (StringUtils.isNull(string))
            string="0";
        bomInfo.setBomName("BOM"+(Integer.parseInt(string)+1));
        List<BomPartstRef> bomPartstRefList = bomInfo.getBomPartstRefList();

        for (BomPartstRef bomPartstRef:bomPartstRefList){
            bomPartstRef.setBomId(bomNo);
            bomPartstRefMapper.insertBomPartstRef(bomPartstRef);
        }
        return bomInfoMapper.insertBomInfo(bomInfo);
    }

    /**
     * 修改bom信息
     * 
     * @param bomInfo bom信息
     * @return 结果
     */
    @Override
    public int updateBomInfo(BomInfo bomInfo)
    {
        return bomInfoMapper.updateBomInfo(bomInfo);
    }

    /**
     * 批量删除bom信息
     * 
     * @param ids 需要删除的bom信息主键
     * @return 结果
     */
    @Override
    public int deleteBomInfoByIds(Long[] ids)
    {

        return bomInfoMapper.deleteBomInfoByIds(ids);
    }

    /**
     * 删除bom信息信息
     * 
     * @param id bom信息主键
     * @return 结果
     */
    @Override
    public int deleteBomInfoById(Long id)
    {
        BomInfo bomInfo = bomInfoMapper.selectBomInfoById(id);
        bomInfoMapper.delbindingBom(bomInfo.getBomNo());
        bomInfoMapper.delbindingBomRef(bomInfo.getBomNo());
        return bomInfoMapper.deleteBomInfoById(id);
    }

    @Override
    public AjaxResult getBulidFloor(String compomentNo) {
        //查询左侧列表
        List<BomBulidFloorVO> bomBulidFloorVOList=bomInfoMapper.selectBomBulidFloor(compomentNo);
        Map<String, List<BomBulidFloorVO>> bomBulidFloorCollect =
                bomBulidFloorVOList.stream().collect(Collectors.groupingBy(bulidFloorVO -> bulidFloorVO.getBuildNo()));
        if (bomBulidFloorCollect!=null && bomBulidFloorCollect.size()>0){
            for (String key:bomBulidFloorCollect.keySet()){
                bomBulidFloorCollect.get(key).sort(new Comparator<BomBulidFloorVO>() {
                                                       @Override
                                                       public int compare(BomBulidFloorVO bomBulidFloorVO1, BomBulidFloorVO bomBulidFloorVO2) {
                                                           Pattern r1 = Pattern.compile("\\d+");
                                                           Matcher m1 = r1.matcher(bomBulidFloorVO1.getFloor());
                                                           int f1=0;
                                                           int f2=0;
                                                           if (m1.find()) {
                                                               f1=Integer.parseInt(m1.group(0));
                                                           }
                                                           Pattern r2 = Pattern.compile("\\d+");
                                                           Matcher m2 = r2.matcher(bomBulidFloorVO2.getFloor());
                                                           if (m2.find()) {
                                                               f2=Integer.parseInt(m2.group(0));
                                                           }
                                                           return f1-f2;
                                                       }
                                                   });
            }
        }

        List<BomInfo> bomInfos=new ArrayList<>();
        if (bomBulidFloorVOList!=null && bomBulidFloorVOList.size()>0){
            bomInfos=bomInfoMapper.selectBomList(bomBulidFloorVOList.get(0).getProjectNo(),compomentNo);
            for (BomInfo bom:bomInfos){
                bom.setPartstInfoList(partstInfoMapper.selectPartstInfoListByBomNo(bom.getBomNo()));
            }
        }

        Map<String,Object> result=new HashMap<>();
        result.put("build",bomBulidFloorCollect);
        result.put("bom",bomInfos);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult bindingBom(List<Integer> projectModelInfoId,String  bomNo) {
        bomInfoMapper.bindingBom(projectModelInfoId,bomNo);
        return AjaxResult.success();

    }

    @Override
    public AjaxResult delBomPartst(Integer id) {
        bomInfoMapper.delBomPartst(id);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult addBomPartst(List<BomPartstRef> bomPartstRefList) {
        for (BomPartstRef bomPartstRef:bomPartstRefList){
            BomPartstRef b=bomPartstRefMapper.selectBomByBomIdAndPartstId(bomPartstRef);
            if (StringUtils.isNull(b)){
                bomPartstRefMapper.insertBomPartstRef(bomPartstRef);
            }else{
                if (b.getCount()!=bomPartstRef.getCount()){
                    bomPartstRef.setId(bomPartstRef.getId());
                    bomPartstRefMapper.updateBomPartstRef(bomPartstRef);
                }
            }
        }

        return AjaxResult.success();
    }
}
